package com.itheima.controller;

import com.itheima.aop.MyLog;
import com.itheima.entity.Classes;
import com.itheima.entity.PageQueryObject;
import com.itheima.entity.Result;
import com.itheima.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/clazzs")
public class ClassController {
    @Autowired
private ClassService classService;

    @MyLog
    @GetMapping
    public Result Serch(PageQueryObject pageRequestParam) {
        return Result.success(classService.Serch(pageRequestParam));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        if (classService.isHasStudent(id)) {
            return Result.error("该班级有学生，不能删除");
        } else {
            classService.delete(id);
            return Result.success();
        }
    }


    @PostMapping
    public Result save(@RequestBody Classes clazz) {
        classService.save(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return Result.success(classService.getById(id));
    }

    @PutMapping
    public Result update(@RequestBody Classes clazz) {
        classService.update(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        return Result.success(classService.list());
    }
}
